package question6;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

/**
 * @FileName MyCalendar
 * @Description
 * @Author Sword
 * @date 2024-11-06
 **/
public class MyCalendar {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 输入年份和月份
        System.out.print("请输入年份（例如：2023）：");
        int year = scanner.nextInt();
        System.out.print("请输入月份（1-12）：");
        int month = scanner.nextInt();

        // 输出日历
        getCalendar(year, month);
    }

    public static void getCalendar(int year, int month) {
        // 创建一个日历对象
        Calendar calendar = new GregorianCalendar(year, month - 1, 1); // 月份从0开始

        // 打印日历头
        System.out.println("日\t一\t二\t三\t四\t五\t六");

        // 获取该月的第一天是星期几
        int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        // 打印空白的前导空格
        for (int i = 1; i < firstDayOfWeek; i++) {
            System.out.print("\t");
        }

        // 打印该月的所有日期
        for (int day = 1; day <= daysInMonth; day++) {
            System.out.print(day + "\t");
            if ((day + firstDayOfWeek - 1) % 7 == 0) {
                System.out.println(); // 换行
            }
        }
    }
}
